﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class dicountDoctorDentist_discountDoctors : System.Web.UI.Page
{
    List<GetDoctorsBYCalendarEventDrIDResult> doctorList = new List<GetDoctorsBYCalendarEventDrIDResult>();

    protected void Page_Load(object sender, EventArgs e)
    {

        FillGrid();

    }

    private void FillGrid()
    {

        #region Non Featured Doctors List
      string DoctorsId = Convert.ToString(Session["StandByDoctorsId"]);
        if (DoctorsId.Length > 0)
        {
            DataSet ds;
            ds = CustomRepository.CustumDataSetFunction(returnSql( DoctorsId), "tbl_drdet");
          
            if (ds.Tables[0].Rows.Count > 0)
            {

                //if (Session["Travel_Dis"].ToString() != "" && Session["Travel_Dis"] != null)  // to make distance column visibility true if search criteria includes Distance Search Functionality
                //{
                //    ds.Tables[0].DefaultView.Sort = " distance,rating desc";
                //    grdSearch.Columns[4].Visible = true;

                //}
                //else
                //{
                //    ds.Tables[0].DefaultView.Sort = " rating desc";
                //    grdSearch.Columns[4].Visible = false;
                ////}

                grdDoctor1.DataSource = ds.Tables[0].DefaultView;
                ////if (pageNoNon > grdSearch.PageCount)
                ////    grdSearch.PageIndex = 1;
                ////else
                ////    grdSearch.PageIndex = pageNoNon;
                grdDoctor1.DataBind();
            }
            //else
            //{
            //    tbl_feat.Visible = false;
            //    lbl_FeaMsg.Text = "No more Featured Links";
            //    lbl_FeaMsg.ForeColor = System.Drawing.Color.Red;
            //}
        }
        ////else
        ////{
        ////    grdSearch.Visible = false;
        ////}
        #endregion


        //DataCalendarDataContext DCD = new DataCalendarDataContext();
        //var query = DCD.CalendarEvents.Select(m => m.doctorID).Distinct();
        
        ////List<CalendarEvent> clanrEvents = CalenderRepository.GetAllCalendarEvents();
        //foreach (var c in query)
        //{
        //    doctorList.AddRange( CalenderRepository.GetDoctorsByCalenderEventDrID(c));
         
        //}
        //   grdDoctor1.DataSource = doctorList;
        //    grdDoctor1.DataBind();

        
        ////grdDoctor1.DataSource= CalenderRepository.GetDoctorsByCalenderDrID();
        ////grdDoctor1.DataBind();
    }

    public string getPhoto(object photo, object sex)
    {
        string p = Convert.ToString(photo);
        if (p.Length > 0)
        {


            if (File.Exists(Request.PhysicalApplicationPath + "\\images\\Dr_images\\" + photo))
            {
                //string str_path = mgr.getThumbnaleImage(Session["strHttp"].ToString() + "images/Dr_images/" + photo, 80, 100, false, "");

                //Ian.........
                string str_path = getThumbnaleImageWithURLFormat(photo.ToString(), 30, 40, false, "", Session["strHTTP"].ToString());
                return str_path;
            }
            else
            {
                if (sex.ToString().ToLower() == "m")
                    return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette-male.gif'>";
                else
                    return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette--female.gif'>";
            }







        }
        else
        {
            if (sex.ToString().ToLower() == "m")
                return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette-male.gif'>";
            else
                return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette--female.gif'>";
        }
    }

    public string getThumbnaleImageWithURLFormat(string imgName, int maxWidth, int maxHeight, bool saveImage, string savePath, string baseURL)
    {
        string s;
        double imgHeight, imgWidth;
        //Response.Write(imgName) ;
        s = Server.MapPath("~/images/dr_images/" + imgName);
        //Response.Write("</br>"+s);
        System.Drawing.Image currentImage = System.Drawing.Image.FromFile(s);
        imgHeight = currentImage.Height;
        imgWidth = currentImage.Width;
        if (imgWidth > maxWidth | imgHeight > maxHeight)
        {
            double deltaWidth = imgWidth - maxWidth;
            double deltaHeight = imgHeight - maxHeight;
            double scaleFactor;
            if (deltaHeight > deltaWidth)
                scaleFactor = maxHeight / imgHeight;
            else
                scaleFactor = maxWidth / imgWidth;
            imgWidth *= scaleFactor;
            imgHeight *= scaleFactor;
        }

        if (saveImage)
        {
            if (savePath == "")
                throw new Exception("must pass a file name");
            currentImage.GetThumbnailImage(Convert.ToInt32(imgWidth), Convert.ToInt32(imgHeight), null, IntPtr.Zero).Save(Server.MapPath(savePath));
            currentImage.Dispose();
        }
        //return currentImage.GetThumbnailImage(Convert.ToInt32(imgWidth), Convert.ToInt32(imgHeight), null, IntPtr.Zero);


        return ("<img src=\"" + baseURL + "images/dr_images/" + imgName + "\"" + " height=\"" + imgHeight + "\" width=\"" + imgWidth + "\" border='0' alt=''/>");
    }

    protected void grdDoctor1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //int doctorId = Convert.ToInt32(e.Row.Cells[1].Text);
                int doctorId = 0;
                string AssignTo = e.Row.Cells[2].Text.ToString();
                if (doctorId != null)
                {
                    LinkButton btnYes = (LinkButton)e.Row.Cells[5].FindControl("btnYes");
                    LinkButton btnNo = (LinkButton)e.Row.Cells[5].FindControl("btnNo");
                    DataCalendarDataContext DCD = new DataCalendarDataContext();
                    var query = DCD.CalendarEvents.Select(m => m.doctorID).Distinct();


                    foreach (var c in query)
                    {
                        if (doctorId == c)
                        {
                            btnYes.Visible = true;
                            btnNo.Visible = false;

                        }
                        else
                        {
                            btnYes.Visible = false;
                            btnNo.Visible = true;

                        }

                    }
                }
            }

        }

        catch (Exception ex)
        {


        }

    }
    protected void grdPremium_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void btnBackToSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(Convert.ToString(Session["strHttps"]) + "dicountDoctorDentist/StandByDoctorSearch.aspx");
    }

    string returnSql(string ids)
    {
        //        //Response.Write(ids); 
        //        Manager mgr = new Manager(true, "ConnectionString");
        string strSql = @"select qu from func_rating()";
        SqlDataReader dr = CustomRepository.CustumExecutQuery(strSql);
        System.String orderClause = "";
        System.String AttachMiles = "";
        //        System.String OrderBy = "id";

        if (Session["Travel_Dis"].ToString() != "" && Session["Travel_Dis"] != null) //  to order the result first by Distance and then by rating Desc / to Calculate Miles
        {
            orderClause = "order by Distance ASC, Rating Desc";
            AttachMiles = " , (dbo.GetDistanceInMiles('" + Session["ZipCode"].ToString() + "','" + Session["City"].ToString() + "','" + Session["State"].ToString() + "',zipcodeforsearch)) as Distance";
        }
        else
        {
            orderClause = "order by Rating Desc";
            AttachMiles = ",'' as distance";
        }

        if (dr.Read())
        {
            strSql = @"select d.city, d.zipcode,  
                        case when (select tbl_primaryspeciality.name from tbl_primaryspeciality where 
    						tbl_primaryspeciality.name not in(Sp.name) and tbl_primaryspeciality.id=d.primaryspecialityid) is null
	                    then 
		                    (select tbl_secondaryspeciality.name from tbl_secondaryspeciality where
						    tbl_secondaryspeciality.id=d.secondaryspecialityid)
	                    else
		                    (select tbl_primaryspeciality.name from tbl_primaryspeciality where 
						    tbl_primaryspeciality.name not in(Sp.name) and tbl_primaryspeciality.id=d.primaryspecialityid)
	                    end 
                        as SubSpec,
                        rtrim((select name from tbl_states where stateId = d.state)) as state,
                        Sp.name as specialityName,
                        CAST((" + dr[0].ToString() + @") as numeric(8,2)) as Rating,d.id as id,fname+' '+isnull(mname,'')+' '+lname as docname,address1+'<br>'+city+', '+state as address,
                         Sp.name+isnull(', '+(select tbl_primaryspeciality.name from tbl_primaryspeciality where 
						tbl_primaryspeciality.name not in(Sp.name) and tbl_primaryspeciality.id=d.primaryspecialityid)
						 ,'')+
						isnull(', ' + (select tbl_secondaryspeciality.name from tbl_secondaryspeciality where
						tbl_secondaryspeciality.name not in(Sp.name,p.name) and
						 tbl_secondaryspeciality.id=d.secondaryspecialityid )
                        ,'')
                        as speciality,
                        isnull(dp.website,'')as website,isnull(dp.photo,'') as photo,d.gender " + AttachMiles + @" 
                        from tbl_doctors d
                        left outer join tbl_primaryspeciality p on p.id=d.primaryspecialityid 
                        left outer join tbl_secondaryspeciality s on s.id=d.secondaryspecialityid 
                        left outer join tbl_DrPremium dp on dp.dr_id=d.id
                        left outer join tbl_speciality sp on sp.id=D.specialityid                        
                        where  d.isdelete=0 and d.id in (" + ids + ")" + orderClause; //Km/Ad        
        }
        dr.Close();
        return strSql;

       
    }
}
